#!/bin/sh
# This regression test is a part of SIPp.
# Each stream goes to the c= address of its own media section, else to
# the session-level one (RFC 4566 5.7): rtp_stream audio and video, and
# pcap plays, against an answer with a session-level c= line and one in
# its video section, and against one with only media-level c= lines.
. "`dirname "$0"`/../functions"

# pcap plays need a raw socket: run as root in a user and network
# namespace of our own if we can, else check rtp_stream only.
if test -z "$SIPP_NETNS" && unshare -rn true 2>/dev/null; then
    SIPP_NETNS=1 exec unshare -rn "$0"
fi
init
uacs=uac.xml
if test -n "$SIPP_NETNS" && ip link set lo up 2>/dev/null &&
        "`get_sipp`" -v | grep -q PCAP; then
    uacs="$uacs uac-pcap.xml"
fi

fail=
for uas in uas.xml uas-media.xml; do
    case $uas in
      uas.xml) audio=127.0.0.1:26470 video=127.0.0.2:26472 ;;
      *) audio=127.0.0.3:26470 video=127.0.0.4:26472 ;;
    esac
    for uac in $uacs; do
        python3 rtp.py $audio $video >rtp.out &
        sippbg -sf $uas -i 127.0.0.1 -p 26460 -m 1 -timeout 10
        sippfg -sf $uac -i 127.0.0.1 -p 26461 -m 1 \
            -min_rtp_port 26480 -max_rtp_port 26499 \
            127.0.0.1:26460 -timeout 10 -timeout_error >/dev/null 2>&1
        status=$?
        wait
        read audio_packets video_packets <rtp.out
        rm -f rtp.out
        if test "${audio_packets:-0}" -eq 0; then
            fail="$uac against $uas: no audio at $audio"
        elif test "${video_packets:-0}" -eq 0; then
            fail="$uac against $uas: no video at $video"
        elif test $status -ne 0; then
            fail="$uac against $uas failed with $status"
        fi
        test -n "$fail" && break 2
    done
done

if test -n "$fail"; then
    fail "$fail"
else
    ok
fi
